<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('物模型列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label style="width: 100px;">物模型名称：</label>
                                <input type="text" name="modelName"/>
                            </li>
                            <li>
                                <label style="width: 100px;">设备ID：</label>
                                <input type="text" name="productId"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:model:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:model:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:model:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:model:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:model:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:model:remove')}]];
        var datas = [[${@dict.getType('iot_model_type')}]];
        var datatypes = [[${@dict.getType('iot_model_data_type')}]];
        var prefix = ctx + "system/model";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "物模型",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'modelId',
                    title: '物模型ID',
                    visible: false
                },
                {
                    field: 'modelName',
                    title: '物模型名称'
                },
                {
                    field: 'productId',
                    title: '设备ID',
                    visible: false
                },
                {
                    field: 'productName',
                    title: '设备名称'
                },
                {
                    field: 'identifier',
                    title: '编码'
                },
                {
                    field: 'type',
                    title: '模型类别',
                    align: "left",
		            formatter: function(value, item, index) {
		            	return $.table.selectDictLabel(datas, item.type);
		            }
                },
                {
                    field: 'datatype',
                    title: '数据类型',
                    align: "left",
		            formatter: function(value, item, index) {
		            	return $.table.selectDictLabel(datatypes, item.datatype);
		            }
                },
                {
                    field: 'specs',
                    title: '数据定义',
                    formatter: function (value, row, index) {
		        		return specsTools(row);
		        	}
                },
                {
                    visible: editFlag == 'hidden' ? false : true,
		        	title: '是否首页显示',
		        	align: 'isTop',
		        	formatter: function (value, row, index) {
		        		return statusTools(row);
		        	}
                },
                {
                    visible: editFlag == 'hidden' ? false : true,
		        	title: '是否实时监测',
		        	align: 'isMonitor',
		        	formatter: function (value, row, index) {
		        		return statusTools(row);
		        	}
                },
                {
                    field: 'remark',
                    title: '备注',
                    visible: false
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.modelId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.modelId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function statusTools(row) {
		    if (row.status == 1) {
    			return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.modelId + '\')"></i> ';
    		} else {
    			return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.modelId + '\')"></i> ';
    		}
		}

		function specsTools(row) {
		    var specs = JSON.parse(row.specs);
		    var htmStr = '<div class="cell"><div data-v-71a82d5e="">';
		    if (row.datatype == 'decimal' || row.datatype == 'integer') {
                htmStr += '<span style="width:50%;display:inline-block;">最大值：<span style="color:#F56C6C">'+specs.max+'</span></span>';
                htmStr += '<span style="width:50%;display:inline-block;">最小值：<span style="color:#F56C6C">'+specs.min+'</span></span>';
                htmStr += '<br>';
                htmStr += '<span style="width:50%;display:inline-block;">步长：<span style="color:#F56C6C">'+specs.step+'</span></span>';
                htmStr += '<span style="width:50%;display:inline-block;">单位：<span style="color:#F56C6C">'+specs.unit+'</span></span>';
                htmStr += '</div>';
                htmStr += '</div>';
    		} else if (row.datatype == 'bool') {
    			htmStr += '<span style="width:50%;display:inline-block;">0：<span style="color:#F56C6C">'+specs.falseText+'</span></span>';
                htmStr += '<span style="width:50%;display:inline-block;">1：<span style="color:#F56C6C">'+specs.trueText+'</span></span>';
    		} else if (row.datatype == 'array') {
    		    htmStr += '<span style="width:50%;display:inline-block;">数组类型：<span style="color:#F56C6C">'+specs.arrayType+'</span></span>';
    		} else if (row.datatype == 'string') {
    		    htmStr += '<span style="width:50%;display:inline-block;">最大长度：<span style="color:#F56C6C">'+specs.maxLength+'</span></span>';
    		} else if (row.datatype == 'enum') {
    		    var enumList = specs.enumList;
    			$.each(enumList, function(i,val){
                    htmStr += '<span style="width:50%;display:inline-block;">'+val.value+'：<span style="color:#F56C6C">'+val.text+'</span></span>';
                    if((i+1)%2 == 0 && i+1 != enumList.length){
                        htmStr += '<br>';
                    }
                });
    		} else {

    		}
    		return htmStr;
		}
    </script>
</body>
</html>